from django.shortcuts import render,HttpResponse,redirect
from django.http import JsonResponse
#csrf_exempt取消csrf_token 验证
from django.views.decorators.csrf import csrf_exempt
import json
from app01 import models
from app01.utils.bootstrapmodel import BootStrapModelForm
from django import forms

class taskModelForm(BootStrapModelForm):
    class Meta:
        model=models.Task
        fields="__all__"
        widgets={
            # 'detail':forms.Textarea,
            'detail':forms.TextInput,
        }


def task_list(request):
    '''任务列表'''
    form=taskModelForm()

    return render(request,'task_list.html',{'form':form})
@csrf_exempt
def task_ajax(request):
    print(request.GET)
    print(request.POST)
    data_dic={"status":True,"data":[11,22,33,44]}
    # json_string=json.dumps(data_dic)
    # # return HttpResponse("成功了")
    # return HttpResponse(json_string)
    # return JsonResponse(data_dic)
    return HttpResponse(json.dumps(data_dic))
@csrf_exempt
def task_add(request):
    # print(request.GET)
    # {'level': ['1'], 'title': ['56'], 'detail': ['345'], 'user': ['3']}
    print(request.POST)
    #1,对用户发送的数据进行校验（modelForm校验）

    form=taskModelForm(data=request.POST)
    if form.is_valid():
        form.save()
        data_dict = {"status": True}
        return HttpResponse(json.dumps(data_dict))
    print(form.errors)
    print(type(form.errors.get_json_data()))
    from django.forms.utils import ErrorDict
    # data_dict = {"status": True,'error':form.errors.get_json_data()}
    data_dict = {"status": False,'error':form.errors}
    return HttpResponse(json.dumps(data_dict,ensure_ascii=False))
